﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class Food
    {
        FosIntelEntities _en = new FosIntelEntities();

        public Food()
        {
            _en.Configuration.ProxyCreationEnabled = false;
        }
        public void SaveFood(fos_Food food)
        {
            if (string.IsNullOrEmpty(food.FoodName)
                || string.IsNullOrEmpty(food.FoodDescription)
                || string.IsNullOrEmpty(food.FoodPrice.ToString()))
                throw new Exception("Fields cannot be empty!");

               
                byte[] convertedImage = Common.convertImageSize(food.FoodPicture);
                food.FoodPicture = convertedImage;
            
                _en.fos_Food.Add(food);
               
          
                _en.SaveChanges();
               
      
        }


        public void UpdateFood(fos_Food food)
        {
            if (string.IsNullOrEmpty(food.FoodName)
                || string.IsNullOrEmpty(food.FoodDescription)
                || string.IsNullOrEmpty(food.FoodPrice.ToString()))
                throw new Exception("Fields cannot be empty!");
            byte[] convertedImage = Common.convertImageSize(food.FoodPicture);
            food.FoodPicture = convertedImage;
            _en.Entry(food).State = System.Data.Entity.EntityState.Modified;    
         
            _en.SaveChanges();
        }



        public fos_Food GetFoodById(long Id)
        {
            fos_Food Food = (from a in _en.fos_Food
                                       where a.Id == Id
                                       && a.IsDeleted == 0
                                       select a).FirstOrDefault();
            return Food;

        }



        // only get food which is available during the start date and end date
        public List<fos_Food> GetFoodBy(string email,string siteId)
        {
            List<fos_Food> listFood;
            // --searching all by site id
            if (email == "-1")
            {
                 long site = Convert.ToInt64(siteId);
                 listFood = (from a in _en.fos_Food
                                           join b in _en.fos_User on a.CreatedBy equals b.Email
                                           join d in _en.fos_SiteLinkCaterer on b.Id equals d.UserId
                                           where a.IsDeleted == 0 && b.IsDeleted ==0
                                           && d.IsDeleted == 0 && d.SiteId == site
                                           && a.StartDate <= DateTime.Now && a.EndDate >= DateTime.Now
                                           select a).ToList<fos_Food>();
                return listFood;
            }

                listFood = (from a in _en.fos_Food
                                       where a.CreatedBy == email
                                       && a.IsDeleted == 0
                                       && a.StartDate <= DateTime.Now && a.EndDate >= DateTime.Now
                                       select a).ToList<fos_Food>();

            return listFood;


        }

        public List<fos_Food> GetFoodBy(string email)
        {
            List<fos_Food> listFood;
           

            listFood = (from a in _en.fos_Food
                        where a.CreatedBy == email
                        && a.IsDeleted == 0
                        select a).ToList<fos_Food>();

            return listFood;


        }

        public List<fos_Food> GetAllFood()
        {
            List<fos_Food> listFood = (from a in _en.fos_Food
                                        select a).ToList<fos_Food>();

            return listFood;


        }

        public List<fos_Food> GetLatestFood()
        {
                List<fos_Food> listFood = (from a in _en.fos_Food
                                       where a.StartDate <= DateTime.Now && a.EndDate >= DateTime.Now  && a.IsDeleted ==0
                                       orderby a.CreatedDate descending
                                       select a).Take(9).ToList();

            return listFood;


        }


        public void DeleteFood(long id)
        {
            fos_Food food = (from a in _en.fos_Food
                             where a.Id == id
                             && a.IsDeleted == 0
                             select a).FirstOrDefault();
            food.IsDeleted = 1;
            _en.SaveChanges();
        }

        public byte[] GetImageBinary(long id)
        {
            byte[] imageBinary = (from a in _en.fos_Food
                                  where a.Id == id
                                  select a.FoodPicture).FirstOrDefault();

            return imageBinary;

        }
    }
}
